# -*- coding: UTF-8 -*-
#!/usr/bin/env python

import cv2

def showFace(x,y,w,h, img):
    x_buttom = x + w
    y_buttom = y + h
    draw_0 = cv2.rectangle(img, (x,y), (x_buttom,y_buttom), (255, 0, 0), 2)
    cv2.imshow("test_imread",draw_0)# 转为RGB显示
    cv2.waitKey()

def convert(face_x, face_y, face_w, face_h, zoom_ratio, imgw, imgh):
    print("input", face_x, face_y, face_w, face_h, zoom_ratio, imgw, imgh)
    left=imgw/2-imgw/(2*zoom_ratio)
    top= imgh/2-imgh/(2*zoom_ratio)
    #W=imgw/zoom_ratio
    #H=imgh/zoom_ratio
    o_face_x=zoom_ratio*(face_x-left)
    o_face_y=zoom_ratio*(face_y-top)
    o_face_w=zoom_ratio*face_w
    o_face_h =zoom_ratio*face_h
    print("output", o_face_x,o_face_y,o_face_w,o_face_h)
    return int(o_face_x),int(o_face_y),int(o_face_w),int(o_face_h)


def showImg():
    for i in range(100):
        image_path="face_pic.png"
        img=cv2.imread(image_path)# np.ndarray BGR uint8
        img_org=cv2.imread(image_path)# np.ndarray BGR uint8
        img_w,img_h = 879,582

        # 显示原始的人脸
        showFace(317,230,212,252,img)

        # TODO: replace the  convert function
        zoom = 1.1+i * 0.1
        x,y,w,h = convert(317,230,212,252,zoom,879,582)
        #return
        # 显示转换之后的人脸
        #crop
        resized_img = cv2.resize(img_org, (int(img_w * zoom), int(img_h * zoom)))
        img_w2 = int((zoom * img_w - img_w)/2)
        img_h2 = int((zoom * img_h - img_h)/2)
        region = resized_img[img_h2:(img_h2 + img_h), img_w2:(img_w2 + img_w)]

        showFace(x,y,w,h, region)

showImg()
